package com.xoa.service.workPlan;

import com.xoa.dao.common.SysCodeMapper;
import com.xoa.dao.sms.SmsMapper;
import com.xoa.dao.workPlan.WorkDetailMapper;
import com.xoa.dao.workPlan.WorkPlanMapper;
import com.xoa.model.common.SysCode;
import com.xoa.model.email.EmailBodyModel;
import com.xoa.model.enclosure.Attachment;
import com.xoa.model.sms.SmsBody;
import com.xoa.model.users.Users;
import com.xoa.model.workPlan.WorkDetailWithBLOBs;
import com.xoa.model.workPlan.WorkPlanWithBLOBs;
import com.xoa.service.department.DepartmentService;
import com.xoa.service.sms.SmsService;
import com.xoa.service.users.UsersService;
import com.xoa.util.*;
import com.xoa.util.common.L;
import com.xoa.util.common.StringUtils;
import com.xoa.util.common.log.FileUtils;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.page.PageParams;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.OutputStream;
import java.util.*;

/**
 * 创建作者:   牛江丽
 * 创建日期:   2017-9-14  16:13
 * 类介绍  :   工作计划service
 * 构造参数:
 */
@Service
public class WorkPlanService {

    @Resource
    private WorkPlanMapper workPlanMapper;
    
    @Autowired
    private UsersService usersService;

    @Resource
    private DepartmentService departmentService;

    @Resource
    private WorkDetailMapper workDetailMapper;

    @Resource
    private SysCodeMapper sysCodeMapper;

    @Resource
    private SmsService smsService;


    @Resource
    ThreadPoolTaskExecutor threadPoolTaskExecutor;

    /**
     * 创建作者:   牛江丽
     * 创建日期:   2017年9月8日 下午16:38:00
     * 方法介绍:   根据条件查询工作计划
     * 参数说明:   @param eduFixAssets
     * 返回值说明:
     */
    public ToJson<WorkPlanWithBLOBs> selectWorkPlanByCond(int output, WorkPlanWithBLOBs workPlanWithBLOBs, HttpServletRequest request, HttpServletResponse response, HttpSession session, Integer page, Integer pageSize, boolean useFlag){
        ToJson<WorkPlanWithBLOBs> json = new ToJson<WorkPlanWithBLOBs>(1, "error");
        try{
            //分页
            PageParams pageParams = new PageParams();
            pageParams.setPage(page);
            pageParams.setPageSize(pageSize);
            pageParams.setUseFlag(useFlag);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("page", pageParams);
            if(workPlanWithBLOBs!=null){
                if(workPlanWithBLOBs.getStatusFlag()!=0){//查询结束计划
                    Date date=new Date();
                    workPlanWithBLOBs.setCurrent(date);
                }

                if(!StringUtils.checkNull(workPlanWithBLOBs.getToId())&& workPlanWithBLOBs.getToId().contains(",")){
                    workPlanWithBLOBs.setToId(workPlanWithBLOBs.getToId().replace(",",""));
                }
                if(!StringUtils.checkNull(workPlanWithBLOBs.getToPersonId())&& workPlanWithBLOBs.getToPersonId().contains(",")){
                    workPlanWithBLOBs.setToPersonId(workPlanWithBLOBs.getToPersonId().replace(",",""));
                }
                if(!StringUtils.checkNull(workPlanWithBLOBs.getParticipator())&& workPlanWithBLOBs.getParticipator().contains(",")){
                    workPlanWithBLOBs.setParticipator(workPlanWithBLOBs.getParticipator().replace(",",""));
                }
            }
            map.put("workPlanWithBLOBs",workPlanWithBLOBs);
            Map<String,Object> maps=new HashMap<>();
            maps.put("workPlanWithBLOBs",workPlanWithBLOBs);
            List<WorkPlanWithBLOBs> workPlanWithBLOBsList=workPlanMapper.selectWorkPlanByCond(map);
            List<WorkPlanWithBLOBs> workplanCount = workPlanMapper.selectWorkPlanByCond(maps);

           /* if(workPlanWithBLOBs!=null){
                if(workPlanWithBLOBs.getStatusFlag()==2){//查询未结束计划
                    workPlanWithBLOBsList=getNoFinished(workPlanWithBLOBs, page, pageSize, useFlag);
                }
            }*/
            Date date=new Date();
            long currentTime= DateFormat.getTime(DateFormat.getStrDate(date));
            long beginTime=0;
            long endTime=0;
            double particiCount=0;//参与人
            int percent=0;//对应计划所占百分比
            Map<String,Object> uidPartParam=new HashMap();
            Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
            Users user= SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(),redisSessionCookie);
            uidPartParam.put("uId",user.getUid());
            for (WorkPlanWithBLOBs temp:workPlanWithBLOBsList) {
                uidPartParam.put("planId",temp.getPlanId());
                int uidPartFlag=workPlanMapper.selManAndPartCount(uidPartParam);
               temp.setUidPartFlag(uidPartFlag);
                //获取状态
                if(temp.getBeginDate()!=null){
                    beginTime=DateFormat.getTime(DateFormat.getStrDate(temp.getBeginDate()));
                }
                if(temp.getEndDate()!=null){
                    endTime=DateFormat.getTime(DateFormat.getStrDate(temp.getEndDate()));
                }
                if(temp.getEndDate()==null){
                    if(currentTime<beginTime){
                        temp.setStatus("1");
                        temp.setStatusName("未开始");
                    }else{
                        temp.setStatus("2");
                        temp.setStatusName("进行中");
                    }
                }else{
                    if(temp.getPublish().equals("1")){
                        if(temp.getSuspendFlag()=="0"){
                            //   temp.setStatus("0");
                            temp.setStatusName("暂停");
                        }else{
                            if(currentTime<beginTime && currentTime<endTime){
                                temp.setStatus("1");
                                temp.setStatusName("未开始");
                            }else if(currentTime>=beginTime && currentTime<=endTime){
                                temp.setStatus("2");
                                temp.setStatusName("进行中");
                            }else if(currentTime>beginTime && currentTime>endTime){
                                temp.setStatus("3");
                                temp.setStatusName("已结束");
                            }
                        }
                    }else{
                        temp.setStatusName("未发布");
                    }
                }
                if(!StringUtils.checkNull(temp.getManager())){
                    particiCount++;
                    temp.setManagerName(usersService.getUserNamesByUserIds(temp.getManager()));
                }
                if(!StringUtils.checkNull(temp.getOpinionLeader())){
                    temp.setOpinionLeaderName(usersService.getUserNamesByUserIds(temp.getOpinionLeader()));
                }
                if(!StringUtils.checkNull(temp.getParticipator())){
                    particiCount+=(temp.getParticipator().split(",")).length-1;
                    temp.setParticipatorName(usersService.getUserNamesByUserIds(temp.getParticipator()));
                }
                if(!StringUtils.checkNull(temp.getToPersonId())){
                    temp.setToPersonName(usersService.getUserNamesByUserIds(temp.getToPersonId()));
                }
                if(!StringUtils.checkNull(temp.getToId())){
                    temp.setToName(departmentService.getDeptNameByDeptId(temp.getToId(),","));
                }
                if(!StringUtils.checkNull(temp.getCreator())){
                    if(usersService.getUsersByuserId(temp.getCreator())!=null){
                        temp.setCreatorName(usersService.getUsersByuserId(temp.getCreator()).getUserName());
                    }
                }
                if(temp.getPlanId()!=null) {
                    WorkDetailWithBLOBs workDetail = new WorkDetailWithBLOBs();
                    workDetail.setPlanId(temp.getPlanId().toString());
                    workDetail.setTypeFlag("0");
                    List<WorkDetailWithBLOBs> workDetailList = workDetailMapper.selWorkDetailSum(workDetail);
                    for (WorkDetailWithBLOBs workDetailWithBLOBs : workDetailList) {
                        if (temp.getPlanId().equals(Integer.parseInt(workDetailWithBLOBs.getPlanId()))) {
                            if (workDetailWithBLOBs.getPercent() != null) {
                                percent = workDetailWithBLOBs.getPercent();
                            }
                        }
                    }
                    if (workDetailList.size() == 0) {
                        percent = 0;
                    }

                    //如果输入负数,默认为0,超过100,默认100
                    if (percent <= 0) {
                        percent = 0;
                    } else if (percent >= 100) {
                        percent = 100;
                    }
                    if (temp.getStatus() != null) {
                        if (temp.getStatus().equals("3")) {
                            percent = 100;
                        }
                    }
                    temp.setProgress(percent);
                }

            }

            if(output==1) {//output==1,则进行导出操作
                HSSFWorkbook tableWork = ExcelUtil.makeExcelHead("工作计划表", 15);
                String[] secondTitles = {"计划名称", "计划内容","开始时间","结束时间","计划类别","开放部门","开放人员","负责人","参与人","创建人","创建时间","状态","备注"};
                HSSFWorkbook excelWork = ExcelUtil.makeSecondHead(tableWork, secondTitles);
                String[] beanProperty = {"name","content","beginDate","endDate","typeName","toName","toPersonName","managerName","participatorName","creatorName","createDate","statusName","remark"};
                HSSFWorkbook workbook = ExcelUtil.exportExcelData(excelWork, workPlanWithBLOBsList, beanProperty);
                response.setContentType("text/html;charset=UTF-8");
                OutputStream out = response.getOutputStream();

                String filename = "工作计划表.xls"; //考虑多语言
                filename = FileUtils.encodeDownloadFilename(filename,
                        request.getHeader("user-agent"));
                response.setContentType("application/vnd.ms-excel");
                response.setHeader("content-disposition",
                        "attachment;filename=" + filename);
                workbook.write(out);
                out.flush();
                out.close();
            }

            //int result=workPlanMapper.selectWorkPlanTotalByCond(workPlanWithBLOBs);
          /*  if(workPlanWithBLOBs!=null){
                if(workPlanWithBLOBs.getStatusFlag()==2){//查询未结束计划
                    result=getNoFinishedCount(workPlanWithBLOBs);
                }
            }*/
         /*  int result=workPlanWithBLOBsList.size();*/

            json.setTotleNum(workplanCount.size());
                json.setObj(workPlanWithBLOBsList);
            json.setMsg("ok");
            json.setFlag(0);
        }catch(Exception e){
            json.setMsg(e.getMessage());
            L.e("WorkPlanService selectWorkPlanByCond:"+e);
            e.printStackTrace();
        }
        return  json;
    }

    /**
     * 创建作者:   牛江丽
     * 创建日期:   2017年9月8日 下午16:48:00
     * 方法介绍:   添加工作计划
     * 参数说明:   @param eduFixAssets
     * 返回值说明:
     */
    @Transactional
    public ToJson<WorkPlanWithBLOBs> insertWorkPlan(WorkPlanWithBLOBs workPlanWithBLOBs,HttpServletRequest request){
        ToJson<WorkPlanWithBLOBs> json = new ToJson<WorkPlanWithBLOBs>(1, "error");
        try{
            Date date=new Date();
            if(workPlanWithBLOBs.getBeginDate()==null){
                workPlanWithBLOBs.setBeginDate(date);
            }
            Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
            Users user= SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(),redisSessionCookie);
            workPlanWithBLOBs.setCreateDate(date);
            workPlanWithBLOBs.setCreator(user.getUserId());
          /*  //截取字符串去掉前面逗号
            String attachmentId = workPlanWithBLOBs.getAttachmentId () ;
            String id = attachmentId .substring ( 1 );
            workPlanWithBLOBs.setAttachmentId (id);


            String attachmentName= workPlanWithBLOBs.getAttachmentName () ;
            String name = attachmentName .substring ( 1 );
            workPlanWithBLOBs.setAttachmentName(name);*/

            if(workPlanWithBLOBs.getAttachmentId ()!=null) {
                int count = workPlanMapper.insertWorkPlan ( workPlanWithBLOBs );
                if (count > 0) {
                    StringBuilder toIds = new StringBuilder();
                    if(!StringUtils.checkNull(workPlanWithBLOBs.getToPersonId())){
                        toIds.append(workPlanWithBLOBs.getToPersonId());
                    }
                    if(!StringUtils.checkNull(workPlanWithBLOBs.getManager())){
                        toIds.append(",").append(workPlanWithBLOBs.getManager());
                    }
                    if(!StringUtils.checkNull(workPlanWithBLOBs.getParticipator())){
                        toIds.append(",").append(workPlanWithBLOBs.getParticipator());
                    }
                    if(!StringUtils.checkNull(workPlanWithBLOBs.getToId())){
                        List<Users> userByDeptIds = usersService.getUserByDeptIds(workPlanWithBLOBs.getToId(), 1);
                        for (Users user2:userByDeptIds
                             ) {
                            toIds.append(",").append(user2.getUserId());
                        }
                    }
                    String sqlType = (String) request.getSession().getAttribute("loginDateSouse");
                    sendSms(workPlanWithBLOBs.getCreator(),toIds.toString(),request.getParameter("remind"),request.getParameter("smsRemind"),sqlType);
                    json.setMsg ( "ok" );
                    json.setFlag ( 0 );
                }
            }
        }catch(Exception e){
            json.setMsg(e.getMessage());
            L.e("WorkPlanService insertWorkPlan:"+e);
            e.printStackTrace();
        }
        return  json;
    }


    /**
     * 创建作者:   牛江丽
     * 创建日期:   2017年9月8日 下午16:48:00
     * 方法介绍:   修改工作计划
     * 参数说明:   @param eduFixAssets
     * 返回值说明:
     */
    @Transactional
    public ToJson<WorkPlanWithBLOBs> updateWorkPlanById(HttpServletRequest request,WorkPlanWithBLOBs workPlanWithBLOBs){
        ToJson<WorkPlanWithBLOBs> json = new ToJson<WorkPlanWithBLOBs>(1, "error");
        int count=0;
        try{
            if(workPlanWithBLOBs.getBeginDate()==null){
                Date date=new Date();
                workPlanWithBLOBs.setBeginDate(date);
            }
                //附件处理
            List<Attachment> attachmentList = new ArrayList<Attachment>();
            if (workPlanWithBLOBs.getAttachmentName() != null && !"".equals(workPlanWithBLOBs.getAttachmentName())) {
                attachmentList = GetAttachmentListUtil.returnAttachment(workPlanWithBLOBs.getAttachmentName(), workPlanWithBLOBs.getAttachmentId(), "xoa" + (String) request.getSession().getAttribute("loginDateSouse"), GetAttachmentListUtil.MODULE_EMAIL);
            }
            workPlanWithBLOBs.setAttachmentList(attachmentList);

            if(!StringUtils.checkNull(workPlanWithBLOBs.getClearEndClearFlag())){
                if(workPlanWithBLOBs.getClearEndClearFlag().equals("1")){
                    WorkPlanWithBLOBs workPlan=workPlanMapper.selectWorkPlanById(workPlanWithBLOBs.getPlanId());

                    if(workPlan.getEndDate()!=null){
                        long endTime=DateFormat.getDateTime(DateFormat.getDatestr(workPlan.getEndDate()));
                        long time=DateFormat.getDateTime(DateFormat.getDatestr(new Date()));
                        if(endTime<=time){
                            count+=workPlanMapper.clearEndDateById(workPlanWithBLOBs.getPlanId());
                        }
                    }
                }
            }
            count+=workPlanMapper.updateWorkPlanById(workPlanWithBLOBs);
            if(count>0){
                json.setMsg("ok");
                json.setFlag(0);
            }
        }catch(Exception e){
            json.setMsg(e.getMessage());
            L.e("WorkPlanService updateWorkPlanById:"+e);
            e.printStackTrace();
        }
        return  json;
    }

    /**
     * 创建作者:   牛江丽
     * 创建日期:   2017年9月8日 下午17:32:00
     * 方法介绍:   根据id删除工作计划
     * 参数说明:   @param id
     * 返回值说明:
     */
    @Transactional
    public ToJson<WorkPlanWithBLOBs> delWorkPlanById(int planId){
        ToJson<WorkPlanWithBLOBs> json = new ToJson<WorkPlanWithBLOBs>(1, "error");
        try{
            int count=workPlanMapper.delWorkPlanById(planId);
            if(count>0){
                json.setMsg("ok");
                json.setFlag(0);
            }
        }catch(Exception e){
            json.setMsg(e.getMessage());
            L.e("WorkPlanService delWorkPlanById:"+e);
            e.printStackTrace();
        }
        return  json;
    }

    /**
     * 创建作者:   牛江丽
     * 创建日期:   2017年9月8日 下午17:32:00
     * 方法介绍:   根据id字符串删除工作计划（多项删除，逗号分隔）
     * 参数说明:   @param id
     * 返回值说明:
     */
    @Transactional
    public ToJson<WorkPlanWithBLOBs> deleteWorkPlanByIds(String planIds) {
        ToJson<WorkPlanWithBLOBs> toJson = new ToJson<WorkPlanWithBLOBs>(1, "error");
        try {
            int count = 0;
            String[] planIdArray = planIds.split(",");
            for (String planId : planIdArray) {
                if (!StringUtils.checkNull(planId)) {
                    count=workPlanMapper.delWorkPlanById(Integer.valueOf(planId));
                }
            }
            if (count > 0) {
                toJson.setFlag(0);
                toJson.setMsg("ok");
            }
        } catch (Exception e) {
            toJson.setMsg(e.getMessage());
            L.e("WorkPlanService deleteWorkPlanByIds:" + e);
        }
        return toJson;
    }


    /**
     * 创建作者:   牛江丽
     * 创建日期:   2017年9月11日 下午14:18:00
     * 方法介绍:   根据id进行查询工作计划
     * 参数说明:   @param id 资产id
     * 返回值说明:
     */
    public ToJson<WorkPlanWithBLOBs> selectWorkPlanById(int planId,HttpServletRequest request){
        ToJson<WorkPlanWithBLOBs> json = new ToJson<WorkPlanWithBLOBs>(1, "error");
        try {
            WorkPlanWithBLOBs workPlanWithBLOBs = workPlanMapper.selectWorkPlanById ( planId );
            //附件处理
            List<Attachment> attachmentList = new ArrayList<Attachment>();
            if (workPlanWithBLOBs.getAttachmentName() != null && !"".equals(workPlanWithBLOBs.getAttachmentName())) {
                attachmentList = GetAttachmentListUtil.returnAttachment(workPlanWithBLOBs.getAttachmentName(), workPlanWithBLOBs.getAttachmentId(), "xoa" + (String) request.getSession().getAttribute("loginDateSouse"), GetAttachmentListUtil.MODULE_EMAIL);
            }
            workPlanWithBLOBs.setAttachmentList(attachmentList);
            //获取状态
            Date date = new Date ();
            long currentTime = DateFormat.getTime ( DateFormat.getStrDate ( date ) );
            long beginTime = 0;
            long endTime = 0;
            if (workPlanWithBLOBs.getBeginDate () != null) {
                beginTime = DateFormat.getTime ( DateFormat.getStrDate ( workPlanWithBLOBs.getBeginDate () ) );
            }
            if (workPlanWithBLOBs.getEndDate () != null) {
                endTime = DateFormat.getTime ( DateFormat.getStrDate ( workPlanWithBLOBs.getEndDate () ) );
            }
            if (workPlanWithBLOBs.getEndDate () == null) {
                if (currentTime < beginTime) {
                    workPlanWithBLOBs.setStatus ( "1" );
                    workPlanWithBLOBs.setStatusName ( "未开始" );
                } else {
                    workPlanWithBLOBs.setStatus ( "2" );
                    workPlanWithBLOBs.setStatusName ( "进行中" );
                }
            } else {
                if (currentTime < beginTime && currentTime < endTime) {
                    workPlanWithBLOBs.setStatus ( "1" );
                    workPlanWithBLOBs.setStatusName ( "未开始" );
                } else if (currentTime > beginTime && currentTime < endTime) {
                    workPlanWithBLOBs.setStatus ( "2" );
                    workPlanWithBLOBs.setStatusName ( "进行中" );
                } else if (currentTime > beginTime && currentTime > endTime) {
                    workPlanWithBLOBs.setStatus ( "3" );
                    workPlanWithBLOBs.setStatusName ( "已结束" );
                }
            }


            if (!StringUtils.checkNull ( workPlanWithBLOBs.getManager () )) {
                workPlanWithBLOBs.setManagerName ( usersService.getUserNamesByUids ( workPlanWithBLOBs.getManager () ) );
            }
            if (!StringUtils.checkNull ( workPlanWithBLOBs.getOpinionLeader () )) {
                workPlanWithBLOBs.setOpinionLeaderName ( usersService.getUserNamesByUids ( workPlanWithBLOBs.getOpinionLeader () ) );
            }
            if (!StringUtils.checkNull ( workPlanWithBLOBs.getParticipator () )) {
                workPlanWithBLOBs.setParticipatorName ( usersService.getUserNamesByUids ( workPlanWithBLOBs.getParticipator () ) );
            }
            if (!StringUtils.checkNull ( workPlanWithBLOBs.getToPersonId () )) {
                workPlanWithBLOBs.setToPersonName ( usersService.getUserNamesByUids ( workPlanWithBLOBs.getToPersonId () ) );
            }
            if (!StringUtils.checkNull ( workPlanWithBLOBs.getToId () )) {
                workPlanWithBLOBs.setToName ( departmentService.getDeptNameByDeptId ( workPlanWithBLOBs.getToId (), "," ) );
            }
            if (!StringUtils.checkNull ( workPlanWithBLOBs.getCreator () )) {
                if (usersService.getUsersByuserId( workPlanWithBLOBs.getCreator () ) != null) {
                    workPlanWithBLOBs.setCreatorName ( usersService.getUsersByuserId ( workPlanWithBLOBs.getCreator () ).getUserName () );
                }
            }

            json.setObject(workPlanWithBLOBs);
            json.setMsg("ok");
            json.setFlag(0);
        }catch(Exception e){
            json.setMsg(e.getMessage());
            L.e("WorkPlanService selectWorkPlanById:"+e);
            e.printStackTrace();
        }
        return  json;
    }


    /**
     * 创建作者:   牛江丽
     * 创建日期:   2017年9月21日 下午09:50:00
     * 方法介绍:   获取未结束的工作计划
     * 参数说明:
     * 返回值说明:
     */
    public List<WorkPlanWithBLOBs> getNoFinished(WorkPlanWithBLOBs workPlanWithBLOBs, Integer page, Integer pageSize, boolean useFlag){
        PageParams pageParams = new PageParams();
        pageParams.setPage(page);
        pageParams.setPageSize(pageSize);
        pageParams.setUseFlag(useFlag);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("page", pageParams);
        map.put("workPlanWithBLOBs",workPlanWithBLOBs);
        List<WorkPlanWithBLOBs> allWorkPlan=workPlanMapper.selectWorkPlanByCond(map);//查询出所有的工作计划

        //查询结束计划
        map.remove("page");
        Date date=new Date();
        workPlanWithBLOBs.setCurrent(date);
        workPlanWithBLOBs.setStatusFlag(1);
        map.put("workPlanWithBLOBs",workPlanWithBLOBs);
        List<WorkPlanWithBLOBs> finishedList=workPlanMapper.selectWorkPlanByCond(map);//查询出已结束的工作计划

        List<WorkPlanWithBLOBs> noFinishedList=new ArrayList<WorkPlanWithBLOBs>();//获得未结束的工作计划
        for(WorkPlanWithBLOBs workPlan:allWorkPlan){//删除结束计划
            int count=0;
            for(WorkPlanWithBLOBs temp:finishedList){
                if(workPlan.getPlanId()==temp.getPlanId()){
                    count++;
                    break;
                }
            }
            if(count==0){
                noFinishedList.add(workPlan);
            }
        }
        return noFinishedList;
    }



    /**
     * 创建作者:   牛江丽
     * 创建日期:   2017年9月21日 下午09:50:00
     * 方法介绍:   获取未结束的工作计划的数量
     * 参数说明:
     * 返回值说明:
     */
    public int getNoFinishedCount(WorkPlanWithBLOBs workPlanWithBLOBs){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("workPlanWithBLOBs",workPlanWithBLOBs);
        List<WorkPlanWithBLOBs> allWorkPlan=workPlanMapper.selectWorkPlanByCond(map);//查询出所有的工作计划

        //查询结束计划
        Date date=new Date();
        workPlanWithBLOBs.setCurrent(date);
        workPlanWithBLOBs.setStatusFlag(1);
        map.put("workPlanWithBLOBs",workPlanWithBLOBs);
        List<WorkPlanWithBLOBs> finishedList=workPlanMapper.selectWorkPlanByCond(map);//查询出已结束的工作计划

        int count=allWorkPlan.size()-finishedList.size();
        return count;
    }

    @Transactional
    public ToJson<WorkPlanWithBLOBs> updatePublish(WorkPlanWithBLOBs workPlanWithBLOBs){
        ToJson<WorkPlanWithBLOBs> json = new ToJson<WorkPlanWithBLOBs> ();

        try{
            if(workPlanWithBLOBs.getEndDate()==null){
                workPlanMapper.updatePublish1(workPlanWithBLOBs);
            }else {
                workPlanMapper.updatePublish(workPlanWithBLOBs);
            }
            json.setFlag ( 0 );
            json.setMsg ( "ok" );
        }catch (Exception e){
            e.printStackTrace ();
            json.setFlag ( 1 );
            json.setMsg ( "err" );
        }

        return json;
    }

    private void sendSms(String fromUserId, String toIds,String remind,String smsRemind,String sqlType) {
        this.threadPoolTaskExecutor.execute(() -> {
            // 发送事务提醒
            SmsBody smsBody = new SmsBody();
            // 发送人
            smsBody.setFromId(fromUserId);
            // 标题（内容）
            smsBody.setContent("工作计划通知!");
            // 发送时间
            smsBody.setSendTime((int) (System.currentTimeMillis() / 1000));
            // 系统代码表中对应的类型id
            SysCode sysCode = new SysCode();
            sysCode.setCodeName("工作计划");
            sysCode.setParentNo("SMS_REMIND");
            SysCode codeNoByNameAndParentNo = sysCodeMapper.getCodeNoByNameAndParentNo(sysCode);
            if (codeNoByNameAndParentNo != null) {
                smsBody.setSmsType(codeNoByNameAndParentNo.getCodeNo());
            }
            // 是否有附件
            smsBody.setIsAttach("0");
            // url
            smsBody.setRemindUrl("workPlan/workPlanQueryIndex");


            //参数含义： smsBody 事务提醒的主要信息类  toIds 提醒用户串(用,拼接 单个推送直接写userId) remind 是否添加提醒 tuisong是否对移动端推送 title context 都是移动端推送参数 sqlType 数据库
            smsService.saveSms(smsBody, toIds, remind, smsRemind, "工作计划通知！", "您好，您有新的工作计划！请及时查看！", sqlType);

        });

    }

}
